<?php
namespace BoxSns\Logic;
class FriendsLogic extends \LogicController
{
    /*****关注一个人**param[$user_id,$to_user_id]****/
    public function follow($param)
    {
        $user_id = $this->user_id;
        $error = Logic("BoxSns", "Member")->check($this->user_id);
        if ($error['error'] > 0) {
            return $error;
        }
        $to_user_id = $param['to_user_id'];
        $model = D("SnsFriends");
        $is_friends = $model->is_friend($user_id, $to_user_id);
        if ($is_friends) {
            return array('error' => 1, 'msg' => '你已关注了他');
        }
        $row = array(
            'user_id' => $user_id,
            'to_user_id' => $to_user_id,
        );
        $r = $model->create_friends($row);
        if ($r) {
            $is_invites = $model->is_friend($to_user_id, $user_id); //如果没有他没有关注我，就是要发送申请啊
            if (!$is_invites && dict('need_apply')) {//发送好友申请
                $row = array(
                    'user_id' => $to_user_id,
                    'to_user_id' => $user_id,
                    'dateline' => date("Y-m-d H:i:s"),
                );
                $r = BoxModel("sns_friends_invite")->add($row);
            }
            return array('error' => 0, 'msg' => '关注成功');
        } else {
            return array('error' => 2, 'msg' => '数据库存操作失败');
        }
    }

    /**********删除一个好友**param[$user_id,$to_user_id]*****/
    public function del_follow($param)
    {
        $error = Logic("BoxSns", "Member")->check($this->user_id);
        if ($error['error'] > 0) {
            return $error;
        }
        $user_id = $this->user_id;
        $to_user_id = $param['to_user_id'];
        $model = BoxModel("SnsFriends");
        $is_friends = $model->is_friend($user_id, $to_user_id);
        if (!$is_friends) {
            return array('error' => 1, 'msg' => '你没有关注他');
        }
        $r = $model->del_friends($user_id, $to_user_id);
        if ($r) {
            return array('error' => 0, 'msg' => '取消成功');
        } else {
            return array('error' => 2, 'msg' => '数据库存操作失败');
        }
    }

    /********我的粉丝param=[user_id,limit]**/
    public function my_fans($param)
    {
        $user_id = $param['user_id'];
        $limit = $param['limit'] ? $param['limit'] : 10;
        if ($user_id == $this->user_id) {
            BoxModel("SnsNotificationCount")->clear($this->user_id, "fans");
        }
        $data = BoxModel("sns_friends")->friend_list("to_user_id='$user_id'", $limit, "fans");
        $model = BoxModel("SnsFriends");
        foreach ($data['list'] as $k => $one) {
            $data['list'][$k]['rel'] = $model->getRel($this->user_id, $one['user_id']);
        }
        // dump($data);
        return $data;
    }

    /********我的关注param[user_id]**/
    public function my_follow($param)
    {
        $user_id = $param['user_id'];
        $limit = $param['limit'] ? $param['limit'] : 10;
        $data = BoxModel("sns_friends")->friend_list("user_id='$user_id'", $limit, "follow");
        $model = BoxModel("SnsFriends");
        foreach ($data['list'] as $k => $one) {
            $data['list'][$k]['rel'] = $model->getRel($this->user_id, $one['user_id']);
        }

        return $data;
    }


}
  
  